﻿using CatUtils.Utils.Currency;
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace CatUtils.Utils.Enhance.Encrypt
{
    /// <summary>
    /// DES加密解密工具
    /// </summary>
    public class DesCryptoTool
    {
        /// <summary>
        /// DES加密字符串   默认UTF8编码
        /// </summary>
        /// <param name="pToEncrypt">待加密的字符串</param>
        /// <param name="sKey">加密密钥,要求为8位</param>
        /// <returns>成功返回解码字符串，失败返回空。  The decode string is returned successfully, and null is returned in case of failure.</returns>

        public static string Encrypt(string pToEncrypt, string sKey)
        {
            return Encrypt(pToEncrypt, sKey, Encoding.UTF8);
        }

        /// <summary>
        /// DES加密字符串
        /// </summary>
        /// <param name="pToEncrypt">待加密的字符串</param>
        /// <param name="sKey">加密密钥,要求为8位</param>

        /// <param name="encoding">配置所用编码</param>
        /// <returns>成功返回解码字符串，失败返回空。  The decode string is returned successfully, and null is returned in case of failure.</returns>

        public static string Encrypt(string pToEncrypt, string sKey, Encoding encoding)
        {
            if (!JudgePassIsCorrect(sKey))
                return "";

            try
            {
                StringBuilder ret = new StringBuilder();
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                byte[] inputByteArray = encoding.GetBytes(pToEncrypt);
                des.Key = encoding.GetBytes(sKey);
                des.IV = encoding.GetBytes(sKey);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                foreach (byte b in ms.ToArray())
                {
                    ret.AppendFormat("{0:X2}", b);
                }
                return ret.ToString();
            }
            catch (Exception ex)
            {
                PrintLog.E(ex);
                return "";
            }
        }

        /// <summary>
        /// DES解密字符串  默认UTF8编码
        /// </summary>
        /// <param name="pToDecrypt"></param>
        /// <param name="sKey"></param>

        /// <returns>成功返回解码字符串，失败返回空。  The decode string is returned successfully, and null is returned in case of failure.</returns>
        public static string Decrypt(string pToDecrypt, string sKey)
        {
            return Decrypt(pToDecrypt, sKey, Encoding.UTF8);
        }

        /// <summary>
        /// DES解密字符串
        /// </summary>
        /// <param name="pToDecrypt">待解密的字符串</param>
        /// <param name="sKey">解密密钥,要求为8位,和加密密钥相同</param>

        /// <param name="encoding">配置所用编码</param>

        /// <returns>成功返回解码字符串，失败返回空。  The decode string is returned successfully, and null is returned in case of failure.</returns>

        public static string Decrypt(string pToDecrypt, string sKey, Encoding encoding)
        {
            if (!JudgePassIsCorrect(sKey))
                return "";

            try
            {
                MemoryStream ms = new MemoryStream();
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
                for (int x = 0; x < pToDecrypt.Length / 2; x++)
                {
                    int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
                    inputByteArray[x] = (byte)i;
                }
                des.Key = encoding.GetBytes(sKey);
                des.IV = encoding.GetBytes(sKey);
                CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                return encoding.GetString(ms.ToArray());
            }
            catch (Exception ex)
            {
                PrintLog.E(ex);
                return "";
            }
        }

        /// <summary>
        /// 提示密码是否合规 符合返回TRUE
        /// </summary>
        /// <param name="sKey"></param>
        /// <returns></returns>
        public static bool JudgePassIsCorrect(string sKey)
        {
            if (sKey.Length != 8)
            {
                PrintLog.E("密码不为八位纯字母数字！\r\n The password is not a mix of eight alphanumeric characters.");
                return false;
            }
            return true;
        }
    }
}